package entry

import (
	"SkyPath/internal/domain/project/constant"
	"SkyPath/internal/domain/project/service"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"net/http"
	"strconv"
)

/*
	和前端对接的入口
*/

func CreateProjects(c *gin.Context, project constant.Projects) {
	projects := &service.Projects{}
	err := projects.CreateProjects(project)
	var httpRes map[string]string
	if errors.Is(err, gorm.ErrDuplicatedKey) {
		httpRes = map[string]string{
			"num": "create project field, project has been exists, please check ProjectID and ProjectName unique key",
		}
	} else {
		httpRes = map[string]string{
			"num": "create project success",
		}
	}
	c.JSON(http.StatusOK, httpRes)
}

func CreateProjectItem(c *gin.Context, items constant.ProjectItems) {
	projects := &service.Projects{}
	err := projects.CreateProjectItems(items)
	var httpRes map[string]string
	if err != nil {
		httpRes = map[string]string{
			"num": "create project item field",
		}
		c.JSON(http.StatusForbidden, httpRes)
	} else {
		httpRes = map[string]string{
			"num": "create project item success",
		}
	}
	c.JSON(http.StatusOK, httpRes)

}

func GetProjects(c *gin.Context, project constant.Projects) {
	projects := &service.Projects{}
	res, err := projects.GetProjects(project)
	var httpRes map[string][]interface{}
	if err != nil {
		httpRes = map[string][]interface{}{
			"projects": nil,
		}
	} else {
		httpRes = map[string][]interface{}{
			"projects": res,
		}
	}

	c.JSON(http.StatusOK, httpRes)
}

func GetProjectItems(c *gin.Context, items constant.ProjectItems) {
	projects := &service.Projects{}
	res, err := projects.GetProjectItems(items)
	var httpRes map[string][]interface{}
	if err != nil {
		httpRes = map[string][]interface{}{
			"projectItems": nil,
		}
	} else {
		httpRes = map[string][]interface{}{
			"projectItems": res,
		}
	}
	c.JSON(http.StatusOK, httpRes)
}

func GetProjectItemsDetail(c *gin.Context, items constant.ProjectItems) {
	projects := &service.Projects{}
	res, err := projects.GetProjectItemsDetail(items)
	var httpRes map[string][]interface{}
	if err != nil {
		httpRes = map[string][]interface{}{
			"projectItems": nil,
		}
	} else {
		httpRes = map[string][]interface{}{
			"projectItems": res,
		}
	}

	c.JSON(http.StatusOK, httpRes)
}

func DelProjects(c *gin.Context, project constant.Projects) {
	projects := &service.Projects{}
	err := projects.DelProjects(project)
	var httpRes map[string]string
	if err != nil {
		httpRes = map[string]string{
			"num": "delete project field",
		}
		c.JSON(http.StatusForbidden, httpRes)
	} else {
		httpRes = map[string]string{
			"projects": "delete project success",
		}
	}
	c.JSON(http.StatusOK, httpRes)
}

func DelProjectItems(c *gin.Context, items constant.ProjectItems) {
	projects := &service.Projects{}
	err := projects.DelProjectItems(items)
	var httpRes map[string]string
	if err != nil {
		httpRes = map[string]string{
			"num": "delete project item field",
		}
		c.JSON(http.StatusForbidden, httpRes)
	} else {
		httpRes = map[string]string{
			"projects": "delete project item success",
		}
	}
	c.JSON(http.StatusOK, httpRes)
}

func ChangeProjects(c *gin.Context, project constant.Projects) {
	projects := &service.Projects{}
	err := projects.ChangeProjects(project)
	var httpRes map[string]string
	if err != nil {
		httpRes = map[string]string{
			"num": "change project field",
		}
		c.JSON(http.StatusForbidden, httpRes)
	} else {
		httpRes = map[string]string{
			"projects": "change project success",
		}
	}
	c.JSON(http.StatusOK, httpRes)
}

func ChangeProjectItems(c *gin.Context, items constant.ProjectItems) {
	projects := &service.Projects{}
	err := projects.ChangeProjectItems(items)
	var httpRes map[string]string
	if err != nil {
		httpRes = map[string]string{
			"num": "change project item field",
		}
		c.JSON(http.StatusForbidden, httpRes)
	} else {
		httpRes = map[string]string{
			"projects": "change project item success",
		}
	}
	c.JSON(http.StatusOK, httpRes)
}

func GetLedger(c *gin.Context) {
	projects := &service.Projects{}
	res, err := projects.GetLedgers()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"status": "bad",
		})
	}
	ledgerDatas, err := projects.StructureLedgerDatas(res)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"status": "bad",
		})
	}
	c.JSON(http.StatusOK, ledgerDatas)
}

func GetLedgerHeaders(c *gin.Context) {
	projects := &service.Projects{}
	res, err := projects.GetLedgerHeaders()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"status": "bad",
		})
	}
	headerRes, err := projects.StructureLedgerHeaders(res)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"status": "bad",
		})
	}
	c.JSON(http.StatusOK, headerRes)
}

func ChangeLedgers(c *gin.Context, ledger map[string]interface{}) {
	projects := &service.Projects{}
	err := projects.ChangeLedgers(ledger)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"status": "bad",
		})
	}
	c.JSON(http.StatusOK, gin.H{
		"status": "change success",
	})
}

func UploadFiles(c *gin.Context) {
	projects := &service.Projects{}
	res, err := projects.UploadFiles(c)
	// 限制上传大小
	c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, constant.MaxUploadMB<<20)
	if err = c.Request.ParseMultipartForm(constant.MaxUploadMB << 20); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "文件太大，最大支持" + strconv.Itoa(constant.MaxUploadMB) + "MB"})
	}
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"message": fmt.Sprintf("%d个文件上传成功", len(res)),
			"files":   res,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"message": "文件上传失败",
			"files":   res,
		})
	}

}

func DownloadFiles(c *gin.Context) {
	//projects := &service.Projects{}
	//res, err := projects.DownloadFiles(c)
}
